const readline = require('readline-sync');

let users = [
    ['zhangsan', 123, 500],
    ['lisi', 456, 1000]
];
let cycle = true;
//登录函数
function login() {
    console.log('请输入账号');
    let id = readline.question();
    console.log('请输入密码');
    let password = readline.question();
    let result = users.filter(function (item) {
        return id == item[0] && password == item[1];
    })

    return result[0];
}
//注册函数
function register() {
    console.log('请输入注册账号');
    let id = readline.question();
    let result = users.some(function (item) {
        return id == item[0]
    })
    if (result) {
        console.log('账号已被注册，请重新输入');
    } else {
        console.log('请输入注册密码');
        let password = readline.question();
        users.push([id, password]);
        console.log('注册成功,请重新登录');
    }
}
//存钱函数
function save_money(user) {
    console.log('请输入存款余额');
    let s = readline.question() - 0;
    console.log(`${user[2]+=s}`);
}
//取款函数
function withdraw_money(user) {
    console.log('请输入取款金额');
    let m = readline.question();
    if (m > user[2]) {
        console.log('余额不足');
    } else {
        console.log(user[2] -= m);
    }
}

function Transfer(user) {
    console.log('请输入收款人');
    let id = readline.question();
    let result = users.filter(function (item) {
        return item[0] == id;
    })
    if (result.length) {
        console.log('输入转账金额');
        let money = readline.question()-0;
        if (money > user[2]) {
            console.log('余额不足');
        } else {
            user[2] -= money;
            result[0][2]+= money;
            console.log(`${user[0]}转款${money},${user[0]}有${user[2]}${result[0][0]}有${result[0][2]}`);
        }
    }
}

function main() {
    let user = null;
    while (cycle) {
        console.log('请选择：1. 登录 2. 注册');
        let opt = readline.question() - 0;
        switch (opt) {
            case 1:
                user = login();
                cycle = false;
                break;
            case 2:
                register();
                break;
        }

    }
    if (user) {
        let flag = true;
        while (flag) {
            console.log(`登录成功，欢迎你，${user[0]} 1. 存 2. 取 3. 查 4. 转账 5. 退`);
            let number = readline.question() - 0;
            switch (number) {
                case 1:
                    //存钱函数
                    save_money(user);
                    break;
                case 2:
                    //取钱函数
                    withdraw_money(user);
                    break;
                case 3:
                    console.log(user);
                    break;
                case 4:
                    Transfer(user);
                    break;
                case 5:
                    console.log('再见');
                    repeat = false;
                    break;
            }
        }
    } else {
        console.log('账号或者密码错误');
    }
}
main();